<template>
  <div class="copyDialog">
    <el-dialog :title="$t('copylayout')" :visible.sync="dialogVisible" :before-close="handleClose" :close-on-click-modal="false" width="30%">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        label-width="130px"
        class="demo-ruleForm"

      >
        <el-form-item :label="$t('layoutName')" prop="layoutName">
          <el-input v-model="form.layoutName"/>
        </el-form-item>
        <el-form-item :label="$t('sourceLayout')" prop="width">
          <el-select v-model="form.LayoutId" clearable @change="changeItem">
            <el-option
              v-for="item in form.layoutList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('choosemachine')" prop="checkAll">
          <el-checkbox
            :indeterminate="isIndeterminate"
            v-model="form.checkAll"
            @change="handleCheckAllChange"
          >{{ $t('selectAll') }}</el-checkbox>
          <div style="margin: 15px 0;"/>
          <el-checkbox-group v-model="form.checked" @change="handleCheckedChange">
            <el-checkbox
              v-for="item in form.checkedList"
              :label="item.eqpName"
              :key="item.id"
            >{{ item.eqpName }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" icon="el-icon-receiving" @click="doSave('ruleForm')">{{ $t('save') }}</el-button>
        <el-button icon="el-icon-delete" @click="resetForm('ruleForm')">{{ $t('reset') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import layoutApi from '@/api/production/layoutDesign'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'CopyDialog',
  components: {},
  mixins: [mySetting],
  data() {
    return {
      dialogVisible: false,
      form: {
        checkAll: false,
        checked: [],
        checkedList: [],
        layoutList: []
      },
      isIndeterminate: false,
      rules: {
        layoutName: [
          { required: true, message: this.$t('layoutNameEmpty'), trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 确定保存
    doSave(val) {
      this.$refs[val].validate(valid => {
        if (!valid) {
          return false
        }
        const obj = {
          layoutName: this.form.layoutName,
          copyLayoutId: this.form.LayoutId,
          selectedEqps: this.form.checkedList.map(ele => ele.eqpId)
        }
        layoutApi.copyList(obj).then(res => {
          this.doMerge(res)
          this.handleClose()
        })
      })
    },
    // 选择下拉框选项
    changeItem(val) {
      this.form.layoutId = val
      this.checked = []
      this.getLayoutEqps()
    },
    // 初始化
    copyInfo() {
      layoutApi.getLayoutNameList().then(res => {
        this.form.layoutList = res.data
      })
    },
    // 选择机台
    getLayoutEqps() {
      layoutApi.getLayoutEqps({ LayoutId: this.form.layoutId }).then(res => {
        this.form.checkedList = res.data
      })
    },
    // 打开弹窗
    openDialog() {
      this.copyInfo()
      this.dialogVisible = true
    },
    // 关闭弹窗
    handleClose() {
      this.resetForm('ruleForm')
      this.form = this.$options.data.call(this).form
      this.dialogVisible = false
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleCheckAllChange(val) {
      this.form.checked = val ? this.form.checkedList.map(ele => ele.eqpName) : []
      this.isIndeterminate = false
    },
    handleCheckedChange(value) {
      const checkedCount = value.length
      this.form.checkAll = checkedCount === this.form.checkedList.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.form.checkedList.length
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
